/**
 * 上传广告---编辑视频广告
 */
function editAd(){
	var adId = getQueryString("adId");
	$.ajax({
		url : '/web-adv/editAd',
        data:{adId:adId},
		type : 'post',
		success : function(data) {

			var resultVo = data.resultVo;
			var picServerPrex = resultVo.picServerPrex;
			var ads = data.bean;
			var duration =   ads.duration+'s'; //广告时长
			$("#advUrl").html(ads.advUrl); //广告链接
			$("#adName").html(ads.adName);	
			$(".adCoverImg").attr("src",picServerPrex+ads.adHomepage);

			//关键字
			var adKeys = ads.adKey.split(" ");
			for(var i =0;i<adKeys.length;i++){
				if(adKeys[i].length !=0){
					$(".adKeyword").append("<span>"+adKeys[i]+"</span>");
				}
			}
			
			//$(".adKeyword").html(ads.adKey);//关键字

			$(".adDescribe").html(ads.description);//描述
			
			//广告属性标签
			var classfyIds = ads.adClassify.split("|");
			for(var i =0;i<classfyIds.length;i++){
				if(classfyIds[i].length != 0){
					console.log(classfyIds[i]);
					var li = "<li><img id=\""+classfyIds[i]+"\" src=\"/web-adv/advWeb/static/images/genres_pic"+classfyIds[i]+".png\" alt=\"\" /></li>";
					$("#adClassify").append(li);
				}
			}
			
			//终端  （mobile，pc，pad，tv）手机、PC、PAD、互联网电视
			var terminalType = ads.terminalType; //终端
			switch(terminalType){
				case 'mobile':
					$("#left :nth-child(1)").attr("src","/web-adv/advWeb/static/images/mobileIcon.png");
					$("#left :nth-child(2)").html("手机");
					$("#size").html(ads.mobileSize);
					$("#adTime").html(duration);
					break;
				case 'pc':
					$("#left :nth-child(1)").attr("src","/web-adv/advWeb/static/images/pcIcon.png");
					$("#left :nth-child(2)").html("PC");
					$("#size").html(ads.pcSize);
					$("#adTime").html(duration);
					break;
				case 'pad':
					$("#left :nth-child(1)").attr("src","/web-adv/advWeb/static/images/padIcon.png");
					$("#left :nth-child(2)").html("PAD");
					$("#size").html(ads.padSize);
					$("#adTime").html(duration);
					break;
				case 'tv':
					$("#left :nth-child(1)").attr("src","/web-adv/advWeb/static/images/tvIcon.png");
					$("#left :nth-child(2)").html("互联网电视");
					$("#size").html(ads.tvSize);
					$("#adTime").html(duration);
					break;
				default:
			}
			$('#videoSuoLueTu').attr("src",picServerPrex+ads.adHomepage);
		},
        error: function(){}        
	});
	
	
}

/**
 *  提交审核
 */
var adHomepage ="";
function updateAd(){
	var adId = getQueryString("adId");
	var terminalType = getQueryString("terminalType");
	var adUrl=$(".adUrl").text();
	var adHomeimage = $("#adHomeimage").attr("src");
	var description = $(".adDescribe").text();
	var adKey="";
	var adKeys = $(".adKeyword").find("span");
	for(var i =0;i<adKeys.length;i++){
		adKey += adKeys.eq(i).html()+"|";
	}

	//广告属性
	var adClassify = "";
	var Achecked=$(".adTarget img");
	$.each(Achecked,function(index){
		//var src = Achecked[index].src;
		//adClassify +=src.substring(src.lastIndexOf(".")-4,src.lastIndexOf("."))+"|";
		adClassify += Achecked[index].id+"|";
	});

	//判断封面是否修改
	if(adHomeimage.indexOf("data:image") !=-1){
		//修改 保存封面图片
		$.ajax({
			url : '/web-adv/updateAd',
			data:{  id:adId,
					adHomepage:adHomepage,
					adKey:adKey,
					description:description,
					terminalType:terminalType,
					adClassify:adClassify,
					advUrl:adUrl
			   },
			type : 'post',
			success : function(data) {
				if(data.status=="1"){
					window.location.href="/web-adv/advWeb/views/uploadAd/videoAd/uploadCheck.html?selectedNav=1&navPos=1&adId="+adId+"";
				}else{
					alert("保存失败！")
				}
			},
			error: function(){}
		});

	}else{
		$.ajax({
			url : '/web-adv/updateAd',
	        data:{  id:adId,		        	
		        	adKey:adKey,
		        	description:description,
		        	terminalType:terminalType,
					adClassify:adClassify,
					advUrl:adUrl
	           },
			type : 'post',
			success : function(data) {
				if(data.status=="1"){
					window.location.href="/web-adv/advWeb/views/uploadAd/videoAd/uploadCheck.html?selectedNav=1&navPos=1&adId="+adId+"";
				}else{
					alert("保存失败！")
				}
			},
	        error: function(){}        
		});
	}
}
function dataURItoBlob(dataURI) {
	var byteString = atob(dataURI.split(',')[1]);
	var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
	var ab = new ArrayBuffer(byteString.length);
	var ia = new Uint8Array(ab);
	for (var i = 0; i < byteString.length; i++) {
		ia[i] = byteString.charCodeAt(i);
	}
	return new Blob([ab], {type: mimeString});
}
var flashImg = {
	index:0,  //图片索引
	leftClick:function(){
		flashImg.index++;
		var imgLength = Math.ceil($("ul.adCoverFilmsWrap").find("li").length/3);
		if(flashImg.index==imgLength){  //归零
			flashImg.index = 0;
		}
		$("ul.adCoverFilmsWrap").stop().animate({left:-flashImg.index*1035-15});
	},
	rightClick:function(){
		flashImg.index--;
		var imgLength = Math.ceil($("ul.adCoverFilmsWrap").find("li").length/3);
		if(flashImg.index<0){  //归零
			flashImg.index = imgLength-1;
		}
		$("ul.adCoverFilmsWrap").stop().animate({left:-flashImg.index*1035-15});
	},
	delegateDom:"",
	delegate:function(ele){
		flashImg.delegateDom = ele;
	},
	isDelegate:function(ele){
		flashImg.delegateDom.parent("li").remove();
		ele.next().trigger("click");
	},
	editDom:"",
	edit:function(ele){
		flashImg.editDom = ele;
	},
	//封面图片
	editUpload:function(e,ele){
		var fs = new FileReader(),files = e.files[0];
		var fileName = files.name;
		if(files.size>10*1024*1024 ||files.size<10*1024 ){
			console.log("======封面====="+files.size)
			alert("单照图片在10K-10M之间，请您重新选择图片！");
		}else {
			if(!flashImg.isSuffix(fileName)) return false;//判断图片格式
			fs.readAsDataURL(files);
			fs.onload = function (event) {
				ele.parent("div.form-inline").next("div").find("img").attr("src",this.result);
				ele.parent("div.form-inline").next("div").find("img").attr("id",fileName);
			}
		}
	},
	upload:function(ele){
		var src = ele.parent("div.form-group").prev("div").find("img").attr("src");
		var id = ele.parent("div.form-group").prev("div").find("img").attr("id");
		if(src){
			flashImg.editDom.parent("div.adCoverFilmsBtnGroup").siblings("img").attr("src",src);
			ele.next().trigger("click");
		};
		if(id){
			flashImg.editDom.parent("div.adCoverFilmsBtnGroup").siblings("img").attr("id",id);
			ele.next().trigger("click");
		}
	},
	uploadPage:function(ele){
		var src = ele.parent("div.form-group").prev("div").find("img").attr("src");
		var fileName = ele.parent("div.form-group").prev("div").find("img").attr("id");
		if(src){
			//flashImg.editDom.siblings("div.adCoverBox").children("img").attr("src",src);
			//上传封面
			var fd = new FormData();
			var blob = dataURItoBlob(src);
			fd.append('file', blob,fileName);
			$.ajax({
				url : '/web-adv/uploadImgs',
				type : 'post',
				data:fd,
				processData: false, // 不会将 data 参数序列化字符串
				contentType: false, // 根据表单 input 提交的数据使用其默认的 contentType
				success : function(data) {
					var data=JSON.parse(data);
					console.log(data.data)
					if(data.status=="1"){
						//保存 所有
						adHomepage = data.data;
						$("#adHomeimage").attr("src",src)
					}else{
						alert("保存失败！");
					}

				},
				error: function(){}
			});

			ele.next().trigger("click");
		}
	},
	setting:function(ele){
		var src = flashImg.settingDom.parent("div.adCoverFilmsBtnGroup").siblings("img").attr("src");
		// console.log(src);
		$("#adHomeimage").attr("src",src);
		ele.next().trigger("click");
	},
	settingDom:"",
	goSetting:function(ele){
		flashImg.settingDom = ele;
	},
	isSuffix:function(ele){
		ele = ele.substring(ele.lastIndexOf(".")+1,ele.length);
		ele=ele.toUpperCase();//jpg、gif、png
		if(ele!="PNG"&&ele!="GIF"&&ele!="JPEG"&&ele!="JPG"){
			alert("请您上传jpg、gif、png格式的图片！")
			return false;
		}
		return true;
	}
};


